﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.SqlClient;

namespace 数据函数库
{
    [Serializable]
    public class 远程操作Data
    {

        public const string IPField = "@IP";

        public const string GREENHOUSEADDRField = "@GreenhouseAddr";

        public const string NOTENUMField = "@NoteNum";

        public const string TIMEField = "@Time";

        public const string 遮阳网Field = "@遮阳网";

        public const string 灌溉阀Field = "@灌溉阀";

        public const string 照明Field = "@照明";

        public const string 排风Field = "@排风";

        private String _IP;

        private Int32 _GreenhouseAddr;

        private Int32 _NoteNum;

        private DateTime _Time;

        private Boolean _遮阳网;

        private Boolean _灌溉阀;

        private Boolean _照明;

        private Boolean _排风;

        public String IP
        {
            get { return _IP; }
            set { _IP = value; }
        }
        public Int32 GreenhouseAddr
        {
            get { return _GreenhouseAddr; }
            set { _GreenhouseAddr = value; }
        }
        public Int32 NoteNum
        {
            get { return _NoteNum; }
            set { _NoteNum = value; }
        }
        public DateTime Time
        {
            get { return _Time; }
            set { _Time = value; }
        }
        public Boolean 遮阳网
        {
            get { return _遮阳网; }
            set { _遮阳网 = value; }
        }
        public Boolean 灌溉阀
        {
            get { return _灌溉阀; }
            set { _灌溉阀 = value; }
        }
        public Boolean 照明
        {
            get { return _照明; }
            set { _照明 = value; }
        }
        public Boolean 排风
        {
            get { return _排风; }
            set { _排风 = value; }
        }
        public 远程操作Data()
        {
            _IP = "";
            _GreenhouseAddr = 0;
            _NoteNum = 0;
            _Time = DateTime.Now;
            _遮阳网 = false;
            _灌溉阀 = false;
            _照明 = false;
            _排风 = false;
        }
    }

    public class 远程操作DataFactory
    {
        public static 远程操作Data Construct(IDataReader reader)
        {
            远程操作Data data = new 远程操作Data();

            try { data.IP = Convert.ToString(reader["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(reader["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(reader["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(reader["Time"]); }
            catch { }

            try { data.遮阳网 = Convert.ToBoolean(reader["遮阳网"]); }
            catch { }

            try { data.灌溉阀 = Convert.ToBoolean(reader["灌溉阀"]); }
            catch { }

            try { data.照明 = Convert.ToBoolean(reader["照明"]); }
            catch { }

            try { data.排风 = Convert.ToBoolean(reader["排风"]); }
            catch { }

            return data;
        }
        public static 远程操作Data Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static 远程操作Data Construct(DataTable dt, int rowIndex)
        {
            远程操作Data data = new 远程操作Data();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.遮阳网 = Convert.ToBoolean(dr["遮阳网"]); }
            catch { }

            try { data.灌溉阀 = Convert.ToBoolean(dr["灌溉阀"]); }
            catch { }

            try { data.照明 = Convert.ToBoolean(dr["照明"]); }
            catch { }

            try { data.排风 = Convert.ToBoolean(dr["排风"]); }
            catch { }

            return data;
        }
        public static 远程操作Data Construct(DataGridView dgv, int rowIndex)
        {
            远程操作Data data = new 远程操作Data();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.遮阳网 = Convert.ToBoolean(dr["遮阳网"]); }
            catch { }

            try { data.灌溉阀 = Convert.ToBoolean(dr["灌溉阀"]); }
            catch { }

            try { data.照明 = Convert.ToBoolean(dr["照明"]); }
            catch { }

            try { data.排风 = Convert.ToBoolean(dr["排风"]); }
            catch { }

            return data;
        }
        public static 远程操作Data Construct(DataRow dr)
        {
            远程操作Data data = new 远程操作Data();

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.遮阳网 = Convert.ToBoolean(dr["遮阳网"]); }
            catch { }

            try { data.灌溉阀 = Convert.ToBoolean(dr["灌溉阀"]); }
            catch { }

            try { data.照明 = Convert.ToBoolean(dr["照明"]); }
            catch { }

            try { data.排风 = Convert.ToBoolean(dr["排风"]); }
            catch { }

            return data;
        }
    }
    public class 远程操作DataDBOption
    {
        public static int Insert(远程操作Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into 远程操作 (");
            sql.Append("IP, ");
            sql.Append("GreenhouseAddr, ");
            sql.Append("NoteNum, ");
            sql.Append("Time, ");
            sql.Append("遮阳网, ");
            sql.Append("灌溉阀, ");
            sql.Append("照明, ");
            sql.Append("排风");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.IP.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.GreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.NoteNum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Time.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.遮阳网.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.灌溉阀.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.照明.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.排风.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 远程操作 ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 远程操作 ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(远程操作Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 远程操作 ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static int Change大棚信息(string ip, uint oldGreenhouseAddr, byte oldNoteNum, uint newGreenhouseAddr, byte newNoteNum)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE    远程操作 SET ");
            sql.Append("GreenhouseAddr =");
            sql.Append(newGreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append("NoteNum =");
            sql.Append(newNoteNum.ToString());
            sql.Append("");
            sql.Append("WHERE     (GreenhouseAddr =");
            sql.Append(oldGreenhouseAddr.ToString());
            sql.Append(") AND (NoteNum =");
            sql.Append(oldNoteNum.ToString());
            sql.Append(") AND (IP = '");
            sql.Append(ip);
            sql.Append("')");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
    }
}